﻿<%@ Page Title="areainfo" Language="C#" MasterPageFile="~/EasyUI.master" AutoEventWireup="true" CodeBehind="List.aspx.cs" Inherits="jnagr.Web.areainfo.List" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
     <script type="text/javascript">
         var selectNode;
         $(function () {
             InitTree();
         });
         function InitTree() {
             $('#p').panel({
                 height: '100%', //高度

                 title: '地区信息'

             });

             $('#tt').tree({
                 url: 'GetArea.ashx?pid=0', //请求数据的页面               
                 lines: true,
                 method: 'post',
                 queryParams: { "action": "queryyb" },
                 formatter: function (node) {
                     return node.bm + " " + node.text;
                 },
                 onBeforeExpand:function(node,param){
                     $('#tt').tree('options').url = "GetArea.ashx?pid=" + node.id;                     
                     
                                   },   
                 
                 onContextMenu: function (e, node) {
                     e.preventDefault();
                     // 查找节点
                     $('#tt').tree('select', node.target);
                     // 显示快捷菜单
                     $('#mm').menu('show', {
                         left: e.pageX,
                         top: e.pageY
                     });
                 }
             });
         }

         function newNode() {
             $('#fm').form('clear');
             var node = $('#tt').tree('getSelected');
             if (node != null) {
                 $('#parentid').val(node.id);
                 selectNode = node;
             }
             $('#dlg').dialog('open').dialog('setTitle', '添加地区');
             document.getElementById("test").value = "add";
         }
         function editNode() {
             var row = $('#tt').tree('getSelected');
             if (row == null) {
                 $.messager.alert("提示", "请选择要修改的数据！", "info");
             }
             if (row) {
                 selectNode = row;
                 $('#aid').val(row.id);
                 $('#aname').val(row.text);
                 $('#abh').val(row.bm);
                 document.getElementById("test").value = "modify";
                 $('#dlg').dialog('open').dialog('setTitle', '修改地区');
             }
         }
         function saveNode() {
             var test = document.getElementById("test").value;
             var f = $('#fm');
             if (test == "add") {
                 f.form('submit', {
                     url: "GetArea.ashx?action=" + test,
                     onSubmit: function () {
                         return $(this).form('validate');
                     },
                     success: function (result) {
                         if (result.indexOf("T") == 0) {
                             $('#dlg').dialog('close');
                             $('#tt').tree('options').url = "GetArea.ashx?pid=0";
                             $('#tt').tree('reload');
                             $.messager.alert("提示", "恭喜您，信息添加成功", "info");
                         }
                         else if ((result.indexOf("F") == 0)) {
                             $.messager.alert("提示", "添加失败，请重新操作！", "info");
                             return;
                         }
                         else if ((result.indexOf("L") == 0)) {
                             $.messager.alert("提示", "长时间没有操作，请重新登录！", "info");
                             top.location = '/index.aspx';
                             return;
                         }
                     }
                 });

             } else {
                 var node = $('#tt').tree('getParent', selectNode.target);
                 $('#fm').form('submit', {
                     url: "GetArea.ashx?action=" + test,
                     onSubmit: function () {
                         return $(this).form('validate');
                     },
                     success: function (result) {
                         if (result.indexOf("T") == 0) {
                             $('#dlg').dialog('close');
                             
                             $('#tt').tree('options').url = "GetArea.ashx?pid=0";
                             $('#tt').tree('reload');
                             $.messager.alert("提示", "恭喜您，信息修改成功", "info");
                         }
                         else {
                             $.messager.alert("提示", "修改失败，请重新操作！", "info");
                             return;
                         }
                     }
                 });
             }
         }
         function removeUser() {
             var test = document.getElementById("test").value = "delete";
             var row = $('#tt').tree('getSelected');
             if (row == null) {
                 $.messager.alert("提示", "请选择要删除的数据！", "info");
             }

             if (row) {
             //    if (!$('#tt').tree('isLeaf', row.target)) {
             //        $.messager.alert('提示', '你选择没有子元素的数据？', "info");

             //    }
             //    else {
                     var node = $('#tt').tree('getParent', row.target);
                     $.messager.confirm('提示', '你确定要删除这条信息吗？', function (r) {
                         if (r) {
                             $('#fm').form('submit', {
                                 url: 'GetArea.ashx?aid=' + row.id + "&action=" + test,
                                 onSubmit: function () {
                                 },
                                 success: function (result) {
                                     if (result.indexOf("T") == 0) {
                                         $('#dlg').dialog('close');
                                         $.messager.alert("提示", "恭喜您，信息删除成功！", "info");
                                         $('#tt').tree('options').url = "GetArea.ashx?pid=0";
                                         $('#tt').tree('reload');
                                     }
                                     else {
                                         $.messager.alert("提示", "删除失败，请重新操作！", "info");
                                         return;
                                     }
                                 }
                             });
                         }
                     })
                 //}
             }
         }
       
    </script>
     <style type="text/css">
        .fitem label {
            width:100px;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div id="p" class="easyui-panel" data-options="iconCls:'icon-save',tools:'#tab_toolbar'"  style="overflow:auto;">   
    <ul id="tt"></ul> 
</div> 
    
 <div id="tab_toolbar" >       
     <a href="#" onclick="newNode();" id="a1" class="icon-add" ></a>
     <a href="javascript:void(0)" onclick="editNode() " class="icon-edit"></a>
     <a href="#" onclick="removeUser();return false;" id="a2" class="icon-cancel" style="margin-right:10px"  ></a>              
    </div>
    <div id="dlg" class="easyui-dialog" style="width: 350px; height: auto; padding: 10px 20px"
        data-options="closed:true,buttons:'#dlg-buttons',shadow:false">
        <form id="fm" method="post">
            <div class="fitem">
               <label>编号：</label>
                <input id="abh" name="abh" class="easyui-validatebox" data-options="required:true" />                          
            </div>
            <div class="fitem">
               <label>名称：</label>
                <input id="aname" name="aname" class="easyui-validatebox" data-options="required:true" />
                <input name="test" id="test" type="hidden" />
                <input name="aid" id="aid" type="hidden" />
                <input name="parentid" id="parentid" type="hidden" />        
            </div>
            
        </form>
    </div>
    <div id="dlg-buttons">
        <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="saveNode()">保存</a>
        <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" onclick="javascript:$('#dlg').dialog('close')">关闭</a>
    </div>
    <div id="mm" class="easyui-menu">
         <div onclick="newNode();" data-options="iconCls:'icon-add'">添加</div>
                    <div onclick="editNode() " data-options="iconCls:'icon-edit'">修改</div>
                    <div  onclick="removeUser();return false;" data-options="iconCls:'icon-cancel'" >删除</div>
    </div>    
   
    
    
     
</asp:Content>
